package cashfinancas

import com.*
import groovy.sql.Sql
/**
 * Descrição: Coloca dados na tela inicial de informações para o usuario 
 * @author alex
 * 
 */
class TelaInicialTagLib {

	def movimentosService
	def dataSource
	def contasPagarList = {attrs ->
	
	def contasPagarMap = movimentosService.contasPagarTela()
	println (contasPagarMap)	
	out <<  "<fieldset class=\"tableField\"><legend>CONTAS PAGAR:</legend>"
	out << "<table class=\"IadTableList\">"
	out << "<thead>"
	out <<	"<thead>"
	out << "<th> ID </th>"
	out << "<th> Descricao </th>"
	out << "<th> Data vencimento </th>"
	out << "<th> Valor </th>"
	out << "</thead>"
	out << "<tbody>"
	contasPagarMap.each{
		if (it.data_vencimento <= new Date()){
			out << "<tr class=\"prop\" style=\"background: none repeat scroll 0 0 #FF8585;\">"
		}else{
			out << "<tr class=\"prop\" style=\"background: #fff;\">"
		}
		out << "<td class=\"value\">"+it.id+"</td>"
		out << "<td class=\"value\">"+it.descricao+"</td>"
		out << "<td class=\"value\">"+it.data_vencimento+"</td>"
		out << "<td class=\"value\">"+it.valor+"</td>"
		
	}
	out << "<tbody>"
	out << "</table>"
	out << "</fieldset>"
	}
	
	def contasReceberList = {attrs ->
	
	def contasReceberMap = movimentosService.contasReceberTela()
	println (contasReceberMap)
	out <<  "<fieldset class=\"tableField\"><legend>CONTAS RECEBER:</legend>"

	out << "<table class=\"IadTableList\">"
	out << "<thead>"
	out <<	"<thead>"
	out << "<th> ID </th>"
	out << "<th> Descricao </th>"
	out << "<th> Data vencimento </th>"
	out << "<th> Valor </th>"
	out << "</thead>"
	out << "<tbody>"
	contasReceberMap.each{
		if (it.data_vencimento <= new Date()){
			out << "<tr class=\"prop\" style=\"background: none repeat scroll 0 0 #FF8585;\">"
		}else{
			out << "<tr class=\"prop\" style=\"background: #fff;\">"
		}
		out << "<td class=\"value\">"+it.id+"</td>"
		out << "<td class=\"value\">"+it.descricao+"</td>"
		out << "<td class=\"value\">"+it.data_vencimento+"</td>"
		out << "<td class=\"value\">"+it.valor+"</td>"
		
	}
	out << "<tbody>"
	out << "</table>"
	out << "</fieldset>"
	}
	def saldoReceber = {
		def sql = new Sql(dataSource)
		
		def QuerySaldoReceber = "select sum(valor) as saldo from contas_receber where baixada=0"
		def saldoReceber = sql.rows(QuerySaldoReceber)
		
		println(saldoReceber)
		 
		out << " R\$ "+ saldoReceber.saldo[0]
	}
	def saldoPagar = {
		def sql = new Sql(dataSource)
		
		def QuerySaldoPagar = "select sum(valor) as saldo from contas_pagar where baixada=0"
		def saldoPagar = sql.rows(QuerySaldoPagar)
		
		out << " R\$ "+ saldoPagar.saldo[0]
	}
	def saldoFinal = {
		def sql = new Sql(dataSource)
		
		def QuerySaldoReceber = "select sum(valor) as saldo from contas_receber where baixada=0"
		def saldoReceber = sql.rows(QuerySaldoReceber)
		
		def QuerySaldoPagar = "select sum(valor) as saldo from contas_pagar where baixada=0"
		def saldoPagar = sql.rows(QuerySaldoPagar)
		
		double saldoP = 0 //Double.parseDouble(saldoPagar.saldo[0].toString()) 
		double saldoR = 0 //Double.parseDouble(saldoReceber.saldo[0].toString())
		
		out << " R\$ "+ saldoR - saldoP
		
	}
}
